<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./OL_SDK/include-openlayers-local.js"></script>
  <script src="./libs/gaode.js"></script>
</head>

<body>
  <button onclick="showState()">实时路况</button>
  <div id="map">
  </div>
  <script>
    var map = new ol.Map({
      target: "map",
      layers: [gaode],
      view: new ol.View({
        projection: 'EPSG:4326',
        center: [114.30, 30.50],
        zoom: 13
      })
    })
    var url = 'http://39.103.151.139:8000/gis/road_state'
    /*
                    flow>1600 red
                    flow>1200 yellow
                    green
                     */
    let source = new ol.source.Vector({
      url,
      format: new ol.format.GeoJSON(),

    })
    let layer = new ol.layer.Vector({
      source,
      style: new ol.style.Style({
        stroke: new ol.style.Stroke({
          color: "black",
          width: 5
        })
      })
    })
    map.addLayer(layer)
    function showState() {
      source.forEachFeature(feature => {
        /* 1、根据flow的值设置样式 */
        let color = null;
        var flow = feature.get("flow");
        if (flow > 1600) {
          color = "red"
        } else if (flow > 1200) {
          color = "yellow"
        } else {
          color = "green"
        }
        let style = new ol.style.Style({
          stroke: new ol.style.Stroke({
            color,
            width: 5
          })
        })
        /* 2、将样式设置给要素 */
        console.log(feature)
        feature.setStyle(style);
      })
    }
  </script>
</body>

</html>